class User < ActiveRecord::Base
  belongs_to :profile
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
  
  accepts_nested_attributes_for :profile
  #validates_presence_of :profile_id #remove temporary

  @@roles_access_levels = {:player => 2, :organizer => 10, :admin => 50}

  def role
    return @@roles_access_levels.key(self[:access_level])
  end

  def role=(the_role)
    self[:access_level] = @@roles_access_levels[the_role]
  end

  def as_json(options = {})
  {
    # name surname faculty group
    id: self.id

  }
  end
  
  def with_profile
    self.build_profile if self.profile.nil?
    self
  end
end

